package educrypt.gui.ctr;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFileChooser;

import educrypt.gui.view.CiphersView;
import educrypt.gui.view.ConsoleBigView;
import educrypt.gui.view.ConsoleSmallView;
import educrypt.gui.view.ConsoleView;
import educrypt.gui.view.CryptanalysisView;
import educrypt.gui.view.MainView;
import educrypt.io.Output;
import educrypt.log.LogMessage;
import educrypt.log.SimpleLogger;

public class ConsoleCtr implements ActionListener, Observer {

	private ConsoleSmallView consoleCipherPanel;
	private ConsoleSmallView consoleCryptanalyisisPanel;
	private ConsoleBigView consoleBigDialog;

	// Flags to know which saveFileView is opened
	private boolean saveFileFromDialogOpened;
	private boolean saveFileFromCipherOpened;
	private boolean saveFileFromCryptanalysisOpened;

	public ConsoleCtr(MainView view) {
		this.consoleCipherPanel = view.getCiphersPanel().getConsolePanel();
		this.consoleCryptanalyisisPanel = view.getCryptanalysisPanel()
				.getConsolePanel();
		this.consoleBigDialog = view.getConsoleBigDialog();

		this.saveFileFromDialogOpened = false;
		this.saveFileFromCipherOpened = false;
		this.saveFileFromCryptanalysisOpened = false;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		if (command.equals(ConsoleView.CLEAR_LOG)) {
			consoleBigDialog.clearLog();
			consoleCipherPanel.clearLog();
			consoleCryptanalyisisPanel.clearLog();
		}
		if (command.equals(ConsoleSmallView.PREVIEW_LOG)) {
			consoleBigDialog.open();
		}
		if (command.equals(ConsoleView.SAVE_LOG)) {
			Component source = (Component) e.getSource();
			if (source.getParent() instanceof ConsoleSmallView) {
				if (source.getParent().getParent() instanceof CiphersView) {
					saveFileFromCipherOpened = true;
					consoleCipherPanel.getSaveFileView().showSaveDialog();
				}
				if (source.getParent().getParent() instanceof CryptanalysisView) {
					saveFileFromCryptanalysisOpened = true;
					consoleCryptanalyisisPanel.getSaveFileView().showSaveDialog();
				}
			} else {
				saveFileFromDialogOpened = true;
				consoleBigDialog.getSaveFileView().showSaveDialog();
			}
		}
		if (command.equals(JFileChooser.APPROVE_SELECTION)) {
			String log = "";
			String filePath = "";
			if (saveFileFromDialogOpened) {
				filePath = consoleBigDialog.getSaveFileView().getSelectedFile()
						.getAbsolutePath();
				log = consoleBigDialog.getLog();
				saveFileFromDialogOpened = false;
			}
			if (saveFileFromCipherOpened) {
				filePath = consoleCipherPanel.getSaveFileView()
						.getSelectedFile().getAbsolutePath();
				log = consoleCipherPanel.getLog();
				saveFileFromCipherOpened = false;
			}
			if (saveFileFromCryptanalysisOpened) {
				filePath = consoleCryptanalyisisPanel.getSaveFileView()
						.getSelectedFile().getAbsolutePath();
				log = consoleCryptanalyisisPanel.getLog();
				saveFileFromCipherOpened = false;
			}
			Output.writePlainText(log, filePath);

		}
		if (command.equals(JFileChooser.CANCEL_SELECTION)) {
			saveFileFromDialogOpened = false;
			saveFileFromCipherOpened = false;
			saveFileFromCryptanalysisOpened = false;
		}
	}

	@Override
	public void update(Observable o, Object arg) {
		assert o instanceof SimpleLogger;
		LogMessage msg = (LogMessage) arg;
		consoleCipherPanel.addLogMessage(msg.getType(), msg.getText());
		consoleCryptanalyisisPanel.addLogMessage(msg.getType(), msg.getText());
		consoleBigDialog.addLogMessage(msg.getType(), msg.getText());
	}
}
